package com.drive.service.serviceimpl;

import com.drive.bean.Member;
import com.drive.bean.Order;
import com.drive.bean.OrderSetting;
import com.drive.constant.MessageConstant;
import com.drive.entity.Result;
import com.drive.mapper.MemberMapper;
import com.drive.mapper.OrderMapper;
import com.drive.mapper.OrderSettingMapper;
import com.drive.service.OrderService;
import com.drive.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 往
 * 科目预约接口实现类
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Result order(Map map) throws Exception {
        //检查当前日期是否进行了预约设置
        String orderDate = (String) map.get("orderDate");
        //将String类型转换成Date类型
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingMapper.findByOrderDate(date);
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //检查预约日期是否约满
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (reservations >= number) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //检查当前学员是否注册
        String telephone = (String) map.get("telephone");
        Member member = memberMapper.findByTelephone(telephone);
        //防止重复预约
        if (member != null) {
            //获取会员id
            Integer memberId = member.getId();
            //获取预约科目id
            Integer subjectId = Integer.parseInt((String) map.get("subjectId"));
            //将信息添加到order类中
            Order order = new Order(memberId, date, subjectId);
            //判断当前学员是否在重复预约
            List<Order> orderList = orderMapper.findByCondition(order);
            if (orderList != null) {
                //当前学员已经预约过
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        }
        //当前学员未预约
        //设置预约人数+1
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingMapper.editReservationsByOrderDate(orderSetting);

        if (member == null) { //当前学员未注册
            //设置member实体属性
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());

            //添加到d_member表中
            memberMapper.add(member);
        }
        //保存信息到预约表中
        //设置Order实体属性
        Order order = new Order(
                member.getId(),
                date,
                (String) map.get("orderType"),
                Order.ORDERTYPE_WEIXIN,
                Integer.parseInt((String) map.get("subjectId")));
        //将数据添加到d_order表中
        orderMapper.add(order);
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    //根据id查询预约信息,包括学员信息/科目信息
    @Override
    public Map findById(Integer id) throws Exception {
        Map  map = orderMapper.findByIdDetail(id);
        if (map!=null){
            //转换日期格式
            Date date = (Date) map.get("orderDate");
            String orderDate = DateUtils.parseDate2String(date);
            map.put("orderDate",orderDate);
        }
        return map;
    }
}
